<template>
  <div>
    <!-- B端订单明细查询-->
    <TopFilter ref="topfrom" :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110" @resetChange="resetChange"></TopFilter>

    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTable
        ref="table"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'records'"
        columnsRef="OrderDetailQueryB"
        :isSelectColumn="false"
        :onColumnsChange="(columns) => (this.columns = columns)"
      >
        <template slot="controls">
          <multiuse-button size="small" type="primary" @click="addHandle()">导 出</multiuse-button>
        </template>
      </FilterTable>
    </Spin>

    <Drawer :visible.sync="detailVisible" :title="'订单信息'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }" width="72%">
      <detailModel :rowData="formData" @close="emitcloseHandler" />
    </Drawer>
    <BaseDialog :visible.sync="issearch" :title="'经销商查询'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <ownerSeacher :rowData="formData" @close="ownerClose" />
    </BaseDialog>
  </div>
</template>
<script>
import { getList, getDealerAccountType, vehicleConfiguration, exportList, exporturl } from '@/api/dealerSale/OrderManagement/OrderDetailQueryB/index.js';
import detailModel from './detailModel';
import ownerSeacher from './ownerSeacher';
import { exportExcelFile } from '@/utils';
import { dictionary } from '@/utils/dictMixin';
export default {
  name: 'OrderDetailQueryB',
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchParams: {
        isDealer: 10041001,
      },
      fetchApi: getList,
      formData: {},
      exportLoading: false,
      issearch: false,
      detailVisible: false,
      isAccountProject: false,
      ProjectId: '',
    };
  },
  components: {
    detailModel,
    ownerSeacher,
  },
  mixins: [dictionary],
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    this.getvehicleList();
    this.getDealerAccountType();
  },
  methods: {
    createdFrom() {
      return [
        {
          type: 'RANGE_DATE',
          label: '提交日期',
          fieldName: 'reportStartDate|reportEndDate',
          placeholder: '请选择',
          initialValue: [],
          dateType: 'exactdaterange',
        },
        {
          type: 'SELECT',
          label: '订单类型',
          fieldName: 'orderType',
          placeholder: '请选择',
          itemList: this.createDictList('2105'),
        },
        {
          type: 'SELECT',
          label: '车辆用途',
          fieldName: 'vehicleUse',
          placeholder: '请输入',
          itemList: this.createDictList('2416'),
        },
        {
          type: 'RANGE_DATE',
          label: '审批日期',
          fieldName: 'checkStartDate|checkEndDate',
          placeholder: '请选择',
          initialValue: [],
          dateType: 'exactdaterange',
        },

        {
          type: 'TEXT_AREA',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '请输入',
          maxlength: '',
          maxRows: 2,
        },
        {
          type: 'INPUT_CASCADER',
          label: '车型选择',
          fieldName: 'brandProductList',
          placeholder: '请选择',
          options: { titles: ['品牌', '车系', '车型', '配置', '选装', '颜色'] },
          itemList: [],
        },
        {
          type: 'SELECT',
          label: '订单明细状态',
          fieldName: 'orderStatus',
          placeholder: '请选择',
          itemList: this.createDictList('2415'),
        },
        {
          type: 'INPUT',
          label: '销售订单编号',
          fieldName: 'doNo',
          placeholder: '请输入',
        },
        {
          type: 'SELECT',
          label: '资金类型',
          fieldName: 'accountId',
          placeholder: '请选择',
          itemList: [],
        },
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: (props) => {
            return <span>{props.row.index + 1}</span>;
          },
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 50,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row, 2)}>
                  详情
                </multiuse-button>
              </div>
            );
          },
        },

        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '订单编号',
          dataIndex: 'orderNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'center',
          width: 200,
        },
        {
          title: '订单管理号',
          dataIndex: 'doNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'center',
          width: 200,
        },
        {
          title: '订单类型',
          dataIndex: 'orderType',
          sorter: true,
          filter: true,
          filterType: 'input',
          filterItems: this.createDictList('2105'),
          render: (p) => {
            return <span>{this.createDictText(p.row.orderType, '2105')}</span>;
          },
        },
        {
          title: '订单紧急度',
          dataIndex: 'emergencyType',
          sorter: true,
          filter: true,
          filterType: 'input',
          filterItems: this.createDictList('2101'),
          render: (p) => {
            return <span>{this.createDictText(p.row.emergencyType, '2101')}</span>;
          },
        },
        {
          title: '车辆用途',
          dataIndex: 'vehicleUse',
          sorter: true,
          filter: true,
          filterType: 'input',
          filterItems: this.createDictList('2416'),
          render: (p) => {
            return <span>{this.createDictText(p.row.vehicleUse, '2416')}</span>;
          },
        },
        {
          title: '订单明细状态',
          dataIndex: 'orderStatus',
          sorter: true,
          filter: true,
          filterType: 'input',
          filterItems: this.createDictList('2415'),
          render: (p) => {
            return <span>{this.createDictText(p.row.orderStatus, '2415')}</span>;
          },
        },
        {
          title: '资金类型',
          dataIndex: 'accountId',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: (p) => {
            return (
              <el-select
                size="mini"
                v-model={p.row.accountId}
                disabled
                onChange={(value) => {
                  p.row.accountId = value;
                }}
              >
                {p.row.accountTypes.map((item) => (
                  <el-option key={item.accountId} label={item.accountTypeName} value={item.accountId} />
                ))}{' '}
              </el-select>
            );
          },
        },
        {
          title: '订单年月',
          dataIndex: 'orderYearMonth',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
         {
          title: '运输方式',
          dataIndex: 'transportType',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: (p) => {
            return <span>{this.createDictText(p.row.transportType, '2409')}</span>;
          },
        },
        {
          title: '收车地址',
          dataIndex: 'address',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '送达城市',
          dataIndex: 'deliveryCityName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '联系人',
          dataIndex: 'linkMan',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '联系电话',
          dataIndex: 'linkPhone',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          align: 'center',
          width: 180,
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '物料编码',
          dataIndex: 'productCode',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '物料描述',
          dataIndex: 'productName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '车系',
          dataIndex: 'seriesName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '车型',
          dataIndex: 'modelName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '配置',
          dataIndex: 'configName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '选装',
          dataIndex: 'optionName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '内饰',
          dataIndex: 'trimName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '外饰',
          dataIndex: 'colorName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '市场指导价',
          dataIndex: 'singleAmount',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '结算价格',
          dataIndex: 'sapSettlementPrice',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '是否使用返利',
          dataIndex: 'isRebate',
          sorter: true,
          filter: true,
          filterType: 'input',
          filterItems: this.createDictList('1004'),
          render: (p) => {
            return <span>{this.createDictText(p.row.isRebate, '1004')}</span>;
          },
        },
        {
          title: '返利使用金额',
          dataIndex: 'rebateAmount',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '单台实付金额',
          dataIndex: 'afterDiscountRebateAmount',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        //todo
        {
          title: '入库日期',
          dataIndex: 'enterWarehouseDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
        },
        {
          title: '提报时间',
          dataIndex: 'reportDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
        },
        {
          title: '审核人',
          dataIndex: 'checkPersonName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '审核时间',
          dataIndex: 'checkDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
        },
        {
          title: '财务审核时间',
          dataIndex: 'financeCheckDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
        },
        {
          title: 'sap出库时间',
          dataIndex: 'sapOutDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
        },
        {
          title: '发运下达时间',
          dataIndex: 'commandDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
        },
        {
          title: '启运时间',
          dataIndex: 'startDeliveryDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
        },
        {
          title: '在途位置信息',
          dataIndex: 'transPosition',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '到店交付时间',
          dataIndex: 'arriveDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
        },
        {
          title: '经销商收车时间',
          dataIndex: 'receiveDates',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        }
      ];
    },
    // 获取车型车系
    async getvehicleList() {
      let res = await vehicleConfiguration(0, { isUnlimited: 10041001 });
      this.topFilterList.find((item) => item.fieldName === 'brandProductList').itemList = res.data;
    },
    async getDealerAccountType() {
      let res = await getDealerAccountType();
      let accountTypeList = res.data.map((x) => ({ text: x.accountTypeName, value: x.accountId }));
      this.topFilterList.find((x) => x.fieldName == 'accountId').itemList = accountTypeList;
    },
    seacherFun(type) {
      this.issearch = true;
    },
    async addHandle() {
      // let json = Object.assign({}, { CODE: 'CODE_0000011' }, this.fetchParams);
      // let res = await exporturl(json);

      // if (res.resultCode == 200) {
      //   this.$router.push('/admin/auth/oem/downloadCenter');
      // }
      this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
        confirmButtonText: '下载',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          let json = this.fetchParams;
          this.exportLoading = true;

          exportList(json)
            .then(response => {
              console.log(response, 'response');
              exportExcelFile(response.data, 'B端订单明细查询');
              this.exportLoading = false;
            })
            .catch(err => {
              console.log(err);
            });
        })
        .catch(() => {
          console.log('456');
          this.$message({
            type: 'info',
            message: '已取消'
          });
        });
    },
    changeHandle(params) {
      this.fetchParams.projectCode = this.ProjectId;
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    editHandle(data) {
      this.formData = data;
      this.detailVisible = true;
    },
    emitcloseHandler() {
      this.detailVisible = false;
    },

    ownerClose(val, data) {
      console.log(this.topFilterList);
      if (data) {
        let dealerCodes = [];
        data.map((x) => {
          dealerCodes.push(x.dealerCode);
        });
        this.topFilterList.find((x) => x.fieldName == 'dealerCode').initialValue = dealerCodes.toString();
      }
      this.issearch = false;
    },

    dealercloseHandler(val) {
      if (val) {
        this.topFilterList.find((x) => x.fieldName == 'dealerName').initialValue = val.dealerName;
        this.dealerCode = val.dealerCode;
      }
      this.issearch = false;
    },
    resetChange(val) {
      this.ProjectId = '';
      this.fetchParams.projectCode = '';
      this.fetchParams = Object.assign({}, this.fetchParams);
    },
  },
};
</script>
